<template>
  <view>
    <view class="tabClass">
      <view style="width: 70%">
        <uni-segmented-control
          :current="current"
          :values="items"
          :style-type="styleType"
          :active-color="activeColor"
          @clickItem="onClickItem"
        />
      </view>
      <text class="jiahaoClass" @click="handledengji">
        <text class="iconfont icon-jiahao" style="font-size: 40rpx"></text>
      </text>
    </view>
    <view
      class="overClass"
      v-if="computedRows.length"
      style="max-height: 100vh"
    >
      <view v-for="item in computedRows" :key="item.id">
        <view class="wrapBox">
          <view class="smallBox">
            <text class="maxwidth"> 危废行业俗称:{{ item.wfTypeName }} </text>
            <!-- <text> 名称:{{ item.wfName }} </text> -->
          </view>
          <view class="smallBox">
            <text> 产出日期:{{ item.produceTime }} </text>
            <text> 登记人:{{ memberStore.userName }} </text>
          </view>
          <view class="smallBox">
            <text> 容器/包装类型:{{ item.wfContainerType }} </text>
            <text> 状态:{{ item.wf_Status }} </text>
          </view>
          <view class="smallBox" v-if="item.wfStatus == 3">
            <text> 转运日期:{{ item.transTime }} </text>
            <text> 申请人:{{ item.applyTransUser }} </text>
          </view>
          <view class="flex-end">
            <button
              @click="handleClick(item)"
              class="commonBtnClass"
              v-if="item.wfStatus == 1"
            >
              编辑
            </button>
            <button
              @tap="handleDelete(item)"
              class="commonBtnClass"
              v-if="item.wfStatus == 1"
            >
              删除
            </button>
            <button
              @click="handleClick(item)"
              class="commonBtnClass"
              v-if="item.wfStatus == 2 || item.wfStatus == 3"
            >
              查看
            </button>
          </view>
        </view>
      </view>
    </view>
    <view v-else class="loadingClass"> 暂无数据 </view>
    <view class="myloginandaplication">
      <l-popup :radius="20" :show="popupShow">
        <view class="width60vw">
          <view style="margin-left: 20rpx; margin-top: 20rpx">{{
            `确定要删除吗？`
          }}</view>
          <view class="classBtn">
            <button
              class="commonBtnClass width150rpx mgright20rpx"
              :disabled="disabled"
              :loading="disabled"
              @click="handledeleteOrRevoke"
            >
              确定
            </button>
            <button class="cancelClass width150rpx" @click="popupShow = false">
              取消
            </button>
          </view>
        </view>
      </l-popup>
    </view>
  </view>
</template>

<script setup>
import { wfRegisterFindPage, wfTypeFindPage, wfRegisterDelete } from "./index";
import { useMemberStore } from "@/stores/modules/member";
import { parseTime } from "@/utils";
const memberStore = useMemberStore();
const current = ref(0);
const items = ref(["登记", "转运中", "已转运"]);
const styleType = ref("button");
const activeColor = ref("#007aff");
const disabled = ref(false);
const popupShow = ref(false);
const currentDate = ref({});
const state = reactive({
  //columns实际没用了
  columns: [
    { name: "wfTypeName", title: "类型", fixed: true },
    { name: "wfName", title: "名称" },
    { name: "wfWeight", title: "产出量" },
    { name: "wfUnit", title: "单位" },
    { name: "produceTime", title: "产出日期" },
    { name: "wfContainerType", title: "容器/包装类型" },
    { name: "wfTypeCode", title: "废物类别" },
    { name: "wfCreateCode", title: "危废代码" },
    { name: "wfTo", title: "去向" },
    { name: "wf_Status", title: "状态" },
  ],
  rows: [],
});
const computedRows = computed(() => {
  if (current.value == 0) {
    return state.rows.filter((item) => item.wfStatus == 1);
  } else if (current.value == 1) {
    return state.rows.filter((item) => item.wfStatus == 2);
  } else if (current.value == 2) {
    return state.rows.filter((item) => item.wfStatus == 3);
  }
});
const allLists = async () => {
  const res = await wfRegisterFindPage(memberStore.userId);
  state.rows = JSON.parse(JSON.stringify(res.data));
  const ress = await wfTypeFindPage();
  state.rows.forEach((item) => {
    item.produceTime = parseTime(item.produceTime, "{y}-{m}-{d}");
    item.wf_Status =
      item.wfStatus == 1
        ? "未转运"
        : item.wfStatus == 2
        ? "审批中"
        : item.wfStatus == 3
        ? "已转运"
        : item.wfStatus == 4
        ? "转运信息暂存"
        : "";
    //获取危废行业俗称
    if (item.wfType) {
      const typeLists = item.wfType.split(",");
      const TypeNames = [];
      typeLists.forEach((wfType) => {
        const obj1 = ress.data.find((item) => item.id == wfType);
        if (obj1) {
          TypeNames.push(obj1.wfTypeName);
        }
      });
      item.wfTypeName = TypeNames.join(",");
    }
    //获取危废行业俗称
  });
  state.rows.reverse();
};
onShow(async () => {
  allLists();
});
const handleClick = (item) => {
  memberStore.RegisterObjViewing = JSON.parse(JSON.stringify(item));
  uni.navigateTo({
    url: `/pages/RegisterViewing/RegisterViewing`,
  });
};
const handleDelete = async (item) => {
  popupShow.value = true;
  currentDate.value = item;
};
//删除
const handledeleteOrRevoke = async () => {
  try {
    disabled.value = true;
    await wfRegisterDelete(currentDate.value.id);
    uni.showToast({ icon: "success", title: "删除成功" });
    popupShow.value = false;
    allLists();
  } finally {
    disabled.value = false;
  }
};
const onClickItem = (e) => {
  console.log(e.currentIndex, 4444);
  if (current.value !== e.currentIndex) {
    current.value = e.currentIndex;
  }
};
const handledengji = () => {
  uni.navigateTo({
    url: "/pages/NewRegistration/NewRegistration?from=MyRegistration",
  });
};
</script>

<style lang="scss" scoped>
.positionClass {
  color: rgb(0, 122, 255);
  display: flex;
  align-items: center;
  margin: 0 40rpx;
  margin-top: 10rpx;
}
</style>